package org.infodavid.common.persistence;

import java.util.List;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.infodavid.common.model.IUserGroup;
import org.infodavid.common.model.criteria.ISearchCriteria;
import org.infodavid.common.persistence.impl.AbstractDataService;
import org.infodavid.common.reflect.InstantiationHelper;

/**
 * The Class GroupDataService.
 */
public abstract class UserGroupDataService extends AbstractDataService<IUserGroup,Long> {

  /** The singleton. */
  private static final UserGroupDataService SINGLETON;

  /** The Constant log. */
  private static final Log LOGGER = LogFactory.getLog(UserGroupDataService.class);

  static {
    try {
      SINGLETON = InstantiationHelper.getInstance().newInstance(UserGroupDataService.class);
    }
    catch (final Exception e) {
      LOGGER.fatal("Can not instantiate the " + UserGroupDataService.class.getName(), e);

      throw new Error(e);
    }
  }

  /**
   * returns the singleton.
   * @return the singletons
   */
  public static UserGroupDataService getInstance() {
    return SINGLETON;
  }

  /**
   * Instantiates a new service.
   */
  protected UserGroupDataService() {
    super();
  }

  /**
   * Do find by name.
   * @param session the session
   * @param value the value
   * @return the group
   * @throws PersistenceException the persistence exception
   */
  public abstract IUserGroup findByName(PersistenceSession session, String value)
      throws PersistenceException;

  /**
   * Find by type.
   * @param session the session
   * @param role the role
   * @param criteria the criteria
   * @param results the list of groups
   * @return the number of results
   * @throws PersistenceException the persistence exception
   */
  public abstract long findByRole(PersistenceSession session, IUserGroup.EUserRole role,
      List<IUserGroup> results, ISearchCriteria criteria) throws PersistenceException;

  /**
   * Find by guest flag.
   * @param session the session
   * @param type the type
   * @param criteria the criteria
   * @param results the list of groups
   * @return the number of results
   * @throws PersistenceException the persistence exception
   */
  public abstract long findByGuest(PersistenceSession session, boolean flag,
      List<IUserGroup> results, ISearchCriteria criteria) throws PersistenceException;

  /**
   * Adds the members.
   * @param session the session
   * @param groupKey the group key
   * @param memebers the members
   * @throws PersistenceException the persistence exception
   */
  public abstract void addMembers(PersistenceSession session, long groupKey, long... members)
      throws PersistenceException;

  /**
   * Removes the members.
   * @param session the session
   * @param groupKey the group key
   * @param memebers the members
   * @throws PersistenceException the persistence exception
   */
  public abstract void removeMembers(PersistenceSession session, long groupKey, long... members)
      throws PersistenceException;
}
